/*
 *******************************************************************************
 *
 * Copyright (C) 2019-2020 Dialog Semiconductor.
 * This computer program includes Confidential, Proprietary Information
 * of Dialog Semiconductor. All Rights Reserved.
 *
 *******************************************************************************
 */

package com.example.suota.suota.global;

import android.util.SparseArray;

import java.util.UUID;

public class SuotaProfile {

    public enum ScanStatus {
        STARTED,
        STOPPED
    }

    public enum ScanFailure {
        FAIL,
        LOCATION_PERMISSION_DENIED,
        SCAN_PERMISSION_DENIED,
        BLE_NOT_SUPPORTED,
        BLUETOOTH_NOT_ENABLED,
        LOCATION_SERVICES_REQUIRED,
        RESCANNING_TOO_SOON
    }

    public enum SuotaManagerStatus {
        CONNECTED,
        DISCONNECTED
    }

    public enum DeviceInfoReadStatus {
        NO_DEVICE_INFO,
        SUCCESS
    }

    public enum SuotaProtocolState {
        ENABLE_NOTIFICATIONS,
        SET_MEMORY_DEVICE,
        SET_GPIO_MAP,
        SEND_BLOCK,
        END_SIGNAL,
        ERROR
    }

    public enum CharacteristicGroup {
        DEVICE_INFO,
        SUOTA_INFO,
        OTHER
    }

    public static class Uuid {
        public static final UUID SUOTA_SERVICE_UUID = UUID.fromString("0000fef5-0000-1000-8000-00805f9b34fb");
        public static final UUID SUOTA_MEM_DEV_UUID = UUID.fromString("8082caa8-41a6-4021-91c6-56f9b954cc34");
        public static final UUID SUOTA_GPIO_MAP_UUID = UUID.fromString("724249f0-5eC3-4b5f-8804-42345af08651");
        public static final UUID SUOTA_MEM_INFO_UUID = UUID.fromString("6c53db25-47a1-45fe-a022-7c92fb334fd4");
        public static final UUID SUOTA_PATCH_LEN_UUID = UUID.fromString("9d84b9a3-000c-49d8-9183-855b673fda31");
        public static final UUID SUOTA_PATCH_DATA_UUID = UUID.fromString("457871e8-d516-4ca1-9116-57d0b17b9cb2");
        public static final UUID SUOTA_SERV_STATUS_UUID = UUID.fromString("5f78df94-798c-46f5-990a-b3eb6a065c88");

        public static final UUID CLIENT_CONFIG_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");

        public static final UUID SUOTA_VERSION_UUID = UUID.fromString("64B4E8B5-0DE5-401B-A21D-ACC8DB3B913A");
        public static final UUID SUOTA_PATCH_DATA_CHAR_SIZE_UUID = UUID.fromString("42C3DFDD-77BE-4D9C-8454-8F875267FB3B");
        public static final UUID SUOTA_MTU_UUID = UUID.fromString("B7DE1EEA-823D-43BB-A3AF-C4903DFCE23C");
        public static final UUID SUOTA_L2CAP_PSM_UUID = UUID.fromString("61C8849C-F639-4765-946E-5C3419BEBB2A");

        public static final UUID SERVICE_DEVICE_INFORMATION = UUID.fromString("0000180a-0000-1000-8000-00805f9b34fb");
        public static final UUID CHARACTERISTIC_MANUFACTURER_NAME_STRING = UUID.fromString("00002A29-0000-1000-8000-00805f9b34fb");
        public static final UUID CHARACTERISTIC_MODEL_NUMBER_STRING = UUID.fromString("00002A24-0000-1000-8000-00805f9b34fb");
        public static final UUID CHARACTERISTIC_SERIAL_NUMBER_STRING = UUID.fromString("00002A25-0000-1000-8000-00805f9b34fb");
        public static final UUID CHARACTERISTIC_HARDWARE_REVISION_STRING = UUID.fromString("00002A27-0000-1000-8000-00805f9b34fb");
        public static final UUID CHARACTERISTIC_FIRMWARE_REVISION_STRING = UUID.fromString("00002A26-0000-1000-8000-00805f9b34fb");
        public static final UUID CHARACTERISTIC_SOFTWARE_REVISION_STRING = UUID.fromString("00002A28-0000-1000-8000-00805f9b34fb");
        public static final UUID CHARACTERISTIC_SYSTEM_ID = UUID.fromString("00002A23-0000-1000-8000-00805f9b34fb");
        public static final UUID CHARACTERISTIC_IEEE_11073 = UUID.fromString("00002A2A-0000-1000-8000-00805f9b34fb");
        public static final UUID CHARACTERISTIC_PNP_ID = UUID.fromString("00002A50-0000-1000-8000-00805f9b34fb");
    }

    public static final int DEFAULT_MTU = 23;

    public static class Constants {
        public static final int REQUEST_ENABLE_BT = 1;
        public static final int REQUEST_LOCATION_SERVICES = 2;

        public static final int LOCATION_PERMISSION_CODE = 0;
        public static final int SCAN_PERMISSION_CODE = 1;
        public static final int STORAGE_PERMISSION_CODE = 2;

        public static final int MEMORY_TYPE_EXTERNAL_I2C = 0x12;
        public static final int MEMORY_TYPE_EXTERNAL_SPI = 0x13;

        public static final int IMAGE_BANK_OLDEST = 0;
        public static final int IMAGE_BANK_1 = 1;
        public static final int IMAGE_BANK_2 = 2;

        public static final int SERVICE_STATUS_OK = 0x02;
        public static final int IMAGE_STARTED = 0x10;

        public static final int SUOTA_END = 0xfe000000;
        public static final int SUOTA_REBOOT = 0xfd000000;
        public static final int SUOTA_ABORT = 0xff000000;

        public static final String BLUETOOTH_PACKAGE_NAME = "com.android.bluetooth";
    }

    public static class Errors {
        public static final int SPOTA_SRV_STARTED = 0x01;
        public static final int SRV_EXIT = 0x03;
        public static final int CRC_MISMATCH = 0x04;
        public static final int PATCH_LENGTH_ERROR = 0x05;
        public static final int EXTERNAL_MEMORY_ERROR = 0x06;
        public static final int INTERNAL_MEMORY_ERROR = 0x07;
        public static final int INVALID_MEMORY_TYPE = 0x08;
        public static final int APPLICATION_ERROR = 0x09;
        public static final int INVALID_IMAGE_BANK = 0x11;
        public static final int INVALID_IMAGE_HEADER = 0x12;
        public static final int INVALID_IMAGE_SIZE = 0x13;
        public static final int INVALID_PRODUCT_HEADER = 0x14;
        public static final int SAME_IMAGE_ERROR = 0x15;
        public static final int EXTERNAL_MEMORY_READ_ERROR = 0x16;

        // Application error codes (must be greater than 255 in order not to conflict with SUOTA error codes)
        public static final int SUOTA_NOT_SUPPORTED = 0xffff;
        public static final int SERVICE_DISCOVERY_ERROR = 0xfffe;
        public static final int GATT_OPERATION_ERROR = 0xfffd;
        public static final int MTU_REQUEST_FAILED = 0xfffc;
        public static final int FIRMWARE_LOAD_FAILED = 0xfffb;
        public static final int INVALID_FIRMWARE_CRC = 0xfffa;
        public static final int UPLOAD_TIMEOUT = 0xfff9;
        public static final int PROTOCOL_ERROR = 0xfff8;
        public static final int NULL_GATT_SINGLETON = 0xfff7;

        public static final SparseArray<String> suotaErrorCodeList = new SparseArray<String>() {{
            put(SPOTA_SRV_STARTED, "SPOTA service started instead of SUOTA.");
            put(SRV_EXIT, "Forced exit of SUOTA service.");
            put(CRC_MISMATCH, "Patch Data CRC mismatch.");
            put(PATCH_LENGTH_ERROR, "Received patch Length not equal to PATCH_LEN characteristic value.");
            put(EXTERNAL_MEMORY_ERROR, "External Memory Error. Writing to external device failed.");
            put(INTERNAL_MEMORY_ERROR, "Internal Memory Error. Not enough internal memory space for patch.");
            put(INVALID_MEMORY_TYPE, "Invalid memory device.");
            put(APPLICATION_ERROR, "Application error.");
            put(INVALID_IMAGE_BANK, "Invalid image bank.");
            put(INVALID_IMAGE_HEADER, "Invalid image header.");
            put(INVALID_IMAGE_SIZE, "Invalid image size.");
            put(INVALID_PRODUCT_HEADER, "Invalid product header.");
            put(SAME_IMAGE_ERROR, "Same Image Error.");
            put(EXTERNAL_MEMORY_READ_ERROR, "Failed to read from external memory device.");
            put(SUOTA_NOT_SUPPORTED, "The remote device does not support SUOTA.");
            put(SERVICE_DISCOVERY_ERROR, "Service discovery error.");
            put(GATT_OPERATION_ERROR, "GATT operation error.");
            put(MTU_REQUEST_FAILED, "MTU request failed.");
            put(FIRMWARE_LOAD_FAILED, "Failed to load the firmware file.");
            put(INVALID_FIRMWARE_CRC, "Firmware CRC validation failed.");
            put(UPLOAD_TIMEOUT, "File upload timeout.");
            put(PROTOCOL_ERROR, "Unexpected behavior while running SUOTA protocol.");
        }};
    }
}
